package com.rui.shangXueTang.select;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : BinarySearch
 * @createTime : [2022/2/16 14:34]
 * @description : [描述该类的功能]
 */

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int index = binarySearch(arr, 5);
        System.out.println(index);
    }

    private static int binarySearch(int[] arr, int target){
        int left = 0;
        int right = arr.length - 1;
        while(left <= right){
            int mid = (right - left) / 2 + left;
            if(arr[mid] < target){
                left = mid + 1;
            }else if(arr[mid] > target){
                right = mid - 1;
            }else {
                return mid;
            }
        }
        return -1;
    }
}
